<?php

declare(strict_types=1);


namespace app\api\controller;

use app\common\model\Make;
use app\common\model\Carousel;
use app\common\model\Area;
use app\common\model\Service;
use app\common\tool\Result;
use think\facade\Cache;
use app\common\controller\Config;

class Index extends Base
{
    /**
     * 获取首页数据
     */
    public function index()
    {
        try {
            $systems = Config::lists();
            $make = Make::field('id,title,sub_title,images,money,type')->order('sort asc')->select();
            foreach ($make as &$v) {
                $v->type_name = self::getType($v->type);
                $v->icon = $v->images ? explode(',', $v->images)[0] : '';
            }
            $data = [
                'home_photo' => $systems['home_photo'],
                'copyright' => $systems['copyright'],
                'banner' => Carousel::where(['position' => 1, 'status' => 1])->field('id,images')->select(),
                'list' => $make,
                'tel'=>Config::lists()['contact_mobile']
            ];
            return Result::success('获取成功', $data);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    public static function getType($type)
    {
        $list =  [
            '1' => '车辆预约',
            '2' => '医护上门',
            '3' => '护理员上门',
        ];
        return $list[$type];
    }
    /**
     * 获取服务内容
     */
    public function makeInfo()
    {
        $id = $this->request->param('id');
        $make = Make::find($id);
        if (empty($make)) {
            return Result::fail('数据不存在');
        }
        $make->type_name = self::getType($make->type);
        $make->content = handleRichText($make->content);
        $make->icon = $make->images ? explode(',', $make->images)[0] : '';
        $make->images = $make->images?explode(',', $make->images):[];
        $make->tel = Config::lists()['contact_mobile'];
        $make->car_list = [
            ['id' => 1, 'title' => '普通转运型'],
            ['id' => 2, 'title' => '医疗转运车'],
            ['id' => 3, 'title' => '监护转运型']
        ];
        $map = [
            'address'=>Config::lists()['address'],
            'lat'=>explode(',',Config::lists()['maplatlng'])[0],
            'lng'=>explode(',',Config::lists()['maplatlng'])[1],
        ];
        $make->map = $map;
        return Result::success('获取成功', $make);
    }
    /**
     * 获取公共协议
     */
    public function communal()
    {
        $type = $this->request->param('type');
        return Result::success('获取成功', ['communal' => handleRichText(Config::lists()[$type] ?? '')]);
    }
    /**
     * 关于我们
     */
    public function about()
    {
        $data = [
            'banner' => Carousel::getList(['position' => 2], false, 'sort', [], 'id,images'),
            'content' => handleRichText(Config::lists()['about'] ?? ''),
        ];
        return Result::success('获取成功', $data);
    }
    /**
     * 服务内容
     */
    public function getServiceList()
    {
        try {
            $list = Service::where('status', 1)->field('id,title,images')->order('sort asc')->select();
            return Result::success('获取成功', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取城市
     */
    public function getCity()
    {
        try {
            $list = Area::where(['level' => 2])->distinct(true)->order('firstchar asc')->column('firstchar');
            $city = [];
            foreach ($list as $k => $v) {
                $cate = Area::where(['level' => 2, 'firstchar' => $v])->field('id,code,city as name,firstchar')->order('jianpin asc')->select();
                $city[$v] = $cate->toArray();
            }
            $hot = Area::where(['level' => 2, 'hot' => 1])->field('id,code,city as name,firstchar')->order('jianpin asc')->select();
            $data['hot'] = $hot->toArray();
            $data['list'] = $city;
            return Result::success('获取成功', $data);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 搜索城市
     */
    public function searchCity()
    {
        try {
            $param = $this->request->param();
            $list = Area::where(['level' => 2])->whereLike('city|pinyin', '%' . $param['keyword'] . '%')->field('id,code,city as name,firstchar')->order('jianpin asc')->select();
            return Result::success('获取成功', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取定位信息
     */
    public function geocoder()
    {
        try {
            $param = $this->request->param();
            $this->validate($param, 'Geocoder');
            $systems = Config::lists();
            $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $param['lat'] . ',' . $param['lng'] . '&key=' . $systems['mapApiKey'] . '&get_poi=1';
            $res = json_decode(file_get_contents($url), true);
            if (empty($res)) {
                return Result::fail('数据不存在');
            } else {
                if ($res['status'] != 0) {
                    return Result::fail($res['message']);
                }
            }
            $cttcode =  Area::where('code', $res['result']['ad_info']['adcode'])->value('parent_code');
            $province_code =  Area::where('code', $cttcode)->value('parent_code');
            $res['result']['ad_info']['city_code'] = $cttcode;
            $res['result']['ad_info']['province_code'] = $province_code;
            $res['result']['ad_info']['address'] = $res['result']['address'];
            return Result::success('获取数据成功！', $res['result']['ad_info']);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
